package com.example.andylau96.res.Function.Cashier;

import android.app.Fragment;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import com.example.andylau96.res.DataBase.CashierDataBase;
import com.example.andylau96.res.Main.MainActivity;
import com.example.andylau96.res.R;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by AndyLau96 on 2016/12/20.
 */
public class jiesuanFragment extends Fragment implements View.OnClickListener, TextWatcher {
    private LinearLayout title;
    private ImageButton re;
    private ListView jiesuan_list;
    private EditText jiesuan_et;
    private SQLiteDatabase db;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.jiesuan,container,false);
        title= (LinearLayout) view.findViewById(R.id.jiesuan_title);
        re= (ImageButton) view.findViewById(R.id.jiesuan_title_return);
        jiesuan_list= (ListView) view.findViewById(R.id.jiesuan_list);
        jiesuan_et= (EditText) view.findViewById(R.id.jiesuan_et);
        jiesuan_et.addTextChangedListener(this);
        re.setOnClickListener(this);
        return view;
    }

    @Override
    public void onClick(View view) {
        getFragmentManager().popBackStack();
    }

    @Override
    public void onStart() {
        super.onStart();
        MainActivity.now=this;
        String theme=null;
        File file=new File("/data/data/com.example.andylau96.res/files/color.txt");
        try {
            InputStreamReader reader=new InputStreamReader(new FileInputStream(file),"UTF-8");
            BufferedReader br=new BufferedReader(reader);
            theme="";
            String temp=null;
            while((temp=br.readLine())!=null)
            {
                theme+=temp;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        title.setBackgroundColor(Color.parseColor(theme));
    }

    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        Log.e("yes","beforeTextChanged-----------");
    }

    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        Log.e("yes","onTextChanged-----------");
    }

    @Override
    public void afterTextChanged(Editable editable) {
        Log.e("yes","afterTextChanged-----------");
        String text=jiesuan_et.getText().toString();
        SimpleAdapter adapter=new SimpleAdapter(getActivity(),getData(text),R.layout.jiesuan_item,new String[]{"id","accounts"},new int[]{
                R.id.jiesuan_id,R.id.jiesuan_accounts});
        jiesuan_list.setAdapter(adapter);
    }
    public List<Map<String,Object>> getData(String s)
    {
        List<Map<String,Object>> list=new ArrayList<>();
        db=new CashierDataBase(getActivity().getApplication()).getReadableDatabase();
        Cursor cr=null;
        if(s.length()!=0)
            cr=db.rawQuery("select *from DinningInfo where dinningID like'%"+s+"%'",null);
        if(cr!=null)
        {
            cr.moveToFirst();
            while (!cr.isAfterLast())
            {
                Map<String, Object> map = new HashMap<>();
                double accounts = cr.getDouble(cr.getColumnIndex("accounts"));
                int id = cr.getInt(cr.getColumnIndex("dinningID"));
                map.put("id", id);
                map.put("accounts", accounts);
                list.add(map);
                cr.moveToNext();
            }
        }
        db.close();
        return list;
    }
}
